package com.chenshu.shop.controller.index;

import com.chenshu.shop.common.Constant;
import com.chenshu.shop.common.SecurityUtil;
import com.chenshu.shop.common.SessionUtil;
import com.chenshu.shop.model.Good;
import com.chenshu.shop.model.User;
import com.chenshu.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-11
 * Time：15:18
 */
@RequestMapping("/index")
@Controller
public class UserController_index {
    @Autowired
    UserService userService;
    @RequestMapping("/reg")
    public String register(User user) {
        userService.register(user);
        return "redirect:/index/login.jsp";
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest request) {
        boolean flag = userService.login(username, password, request);
        if (flag) {
            return "forward:/index/index";
        } else {
            return "redirect:/index/login.jsp";
        }
    }

    @ResponseBody
    @RequestMapping("/checkUsername")
    public String checkUsername(String username) {
        Boolean flag = userService.checkUsername(username);
        if (!flag) {
            return "cunzai";
        }
        return "";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            User user = (User) session.getAttribute(Constant.SESSION_USER_INFO_KEY);
            if (user != null) {
                session.removeAttribute(Constant.SESSION_USER_INFO_KEY);
                session.removeAttribute(Constant.USER_CART_COUNT);
            }
        }
        return "redirect:/index/login.jsp";
    }

    @RequestMapping("/password")
    public String password(HttpServletRequest request) {
        request.setAttribute("msg", "请修改您的密码！");
        return "forward:/index/password.jsp";
    }

    @RequestMapping("/passwordUpdate")
    public String passwordUpdate(HttpServletRequest request, String password, String passwordNew) {
        User user = SessionUtil.getLoginUser(request);
        assert user != null;
        user = userService.getUserById(user.getId());
        boolean flag = SecurityUtil.decrypt(password, user.getPassword());
        if (!flag) {
            //验证失败
            request.setAttribute("msg", "原密码错误，请重新再试！");
            return "forward:/index/password.jsp";
        } else {
            //验证成功，开始修改密码
            userService.updateUser(user.getId(), passwordNew);
            return "redirect:/index/logout";
        }
    }

    @RequestMapping("/address")
    public String JumpAddress(HttpServletRequest request) {
        request.setAttribute("msg", "请修改您的收获地址");
        return "forward:/index/address.jsp";
    }

    @RequestMapping("addressUpdate")
    public String addressUpdate(HttpServletRequest request, User user) {
        User loginUser = SessionUtil.getLoginUser(request);
        assert loginUser != null;
        user.setId(loginUser.getId());
        userService.updateUserInfo(user);
        //更新session的值
        loginUser.setAddress(user.getAddress());
        loginUser.setName(user.getName());
        loginUser.setPhone(user.getPhone());
        request.setAttribute("msg", "修改收获地址成功！");
        return "forward:/index/address.jsp";
    }
}
